import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'



/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '主页', icon: 'dashboard' }
    }]
  },
 
  {
    path: '/tenant',
    component: Layout,
    id:1,
    children: [
      {
        path: 'index',
        name: 'tenant',
        id:11,
        component: () => import('@/views/tenant/index'),
        meta: { title: '租户管理', icon: 'form' }
      } ,
      {
        path: 'authorize',
        id:12,
        component: () => import('@/views/tenant/authorize'),
        hidden: true
      },
    ]
  },
    {
    path: '/system',
    component: Layout,
    id:3,
    meta: { title: '系统管理', icon: 'form' },
    children: [
       {
          path: 'role',
          name: 'role',
          id:31,
          component: () => import('@/views/role/index'),
          meta: { title: '角色管理', icon: 'form' }
        },
        {
          path: '/user',
          name: 'user',
          id:32,
          component: () => import('@/views/user/index'),
          meta: { title: '用户管理', icon: 'form' }
        }
         
    ]
  },

 
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: filterRouter()
})
function filterRouter(){
   var f = filters(constantRoutes)
   console.log(f)
   return f
}
function filters(children){
  var modulestr=sessionStorage.getItem('AUTHORITY_ARR')
  var modules = JSON.parse(modulestr);
  console.log(modules)
  if(modules==null){
    modules=[]
  }
  return children.filter(item =>{

    if(item.children!=null){
      item.children=filters(item.children)
    }
    
    if(item.id){
       if( !modules.includes(item.id)){
          item.hidden=true
       }
      return modules.includes(item.id)
    }else{
      return true;
    }
  });

}
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
